<!DOCTYPE html>
<html>
<head>
    <title></title>
    <meta charset="utf-8">
    <link rel="stylesheet" href="../vendor/bootstrap/css/bootstrap.css"/>
    <link rel="stylesheet" href="../dist/css/bootstrapValidator.css"/>

    <style>
        #sub{    text-align: center;
            margin-top: 67px;}
        .form-control{
            width: 230px !important;
            margin-bottom: 10px;
        }
    </style>
    <script type="text/javascript" src="../vendor/jquery/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="../vendor/bootstrap/js/bootstrap.min.js"></script>
    <script type="text/javascript" src="../dist/js/bootstrapValidator.js"></script>
    <script type="text/javascript" src="../js/Jquery.ETTool-1.0.js"></script>
    <script type="text/javascript" src="../layer/layer.js"></script>
</head>
<body>
    <div class="container">
        <div class="row">
            <!-- form: -->
            <section>
                <div class="col-sm-12">
                    <div class="page-header">

                    </div>

                    <form id="defaultForm" method="post" class="form-horizontal">
                        <div class="form-group" >
                            <label class="col-sm-2 control-label">字段编码</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="fieldCode" placeholder="字段编码"/>
                            </div>
                            <label class="col-sm-2 control-label">字段名称</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="fieldName" placeholder="字段名称"/>
                            </div>
                            <label class="col-sm-2 control-label">创建人ID</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="crtUserId" placeholder="创建人ID"/>
                            </div>
                            <label class="col-sm-2 control-label">创建人</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="crtUserName" placeholder="创建人"/>
                            </div>
                            <label class="col-sm-2 control-label">最后维护人ID</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="mntUserId" placeholder="最后维护人ID"/>
                            </div>
                            <label class="col-sm-2 control-label">最后维护人</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="mntUserName" placeholder="最后维护人"/>
                            </div>
                            <label class="col-sm-2 control-label">描述</label>
                            <div class="col-sm-4">
                                <input type="text" class="form-control" name="appName" id="des" placeholder="描述"/>
                            </div>

                        </div>
                        <div class="form-group" id="sub">
                            <div class="col-lg-9 col-lg-offset-3">
                                <button type="submit" class="btn btn-primary" name="signup" value="Sign up" id="signup">提交</button>
                                <button type="button" class="btn btn-info" id="resetBtn">重置</button>
                            </div>
                        </div>
                    </form>

                </div>
            </section>
            <!-- :form -->
        </div>
    </div>

<script type="text/javascript">
$(document).ready(function() {
    // Generate a simple captcha
    var code = localStorage.getItem("appCode")
    console.log(code)
    if(localStorage.getItem("arrgunsTHF") != ""){
        var str = localStorage.getItem("arrgunsTHF")
        var arr = str.split(",")
        $("#defaultForm input").each(function(index){
            $(this).val(arr[index+1])
        })
        localStorage.setItem("arrgunsTHF","")
    }
    if(localStorage.getItem("edit") =="true"){
        $("#fieldCode").attr("readonly","readonly")
        $("#fieldName").attr("readonly","readonly")
    }
    $('#defaultForm').bootstrapValidator({
//        live: 'disabled',
        message: 'This value is not valid',
        feedbackIcons: {
            valid: 'glyphicon glyphicon-ok',
            invalid: 'glyphicon glyphicon-remove',
            validating: 'glyphicon glyphicon-refresh'
        },
        fields: {
            firstName: {
                validators: {
                    notEmpty: {
                        message: 'The first name is required and cannot be empty'
                    }
                }
            },
            lastName: {
                validators: {
                    notEmpty: {
                        message: 'The last name is required and cannot be empty'
                    }
                }
            },
            username: {
                message: 'The username is not valid',
                validators: {
                    notEmpty: {
                        message: 'The username is required and cannot be empty'
                    },
                    stringLength: {
                        min: 6,
                        max: 30,
                        message: 'The username must be more than 6 and less than 30 characters long'
                    },
                    regexp: {
                        regexp: /^[a-zA-Z0-9_\.]+$/,
                        message: 'The username can only consist of alphabetical, number, dot and underscore'
                    },
                    remote: {
                        url: 'remote.php',
                        message: 'The username is not available'
                    },
                    different: {
                        field: 'password',
                        message: 'The username and password cannot be the same as each other'
                    }
                }
            },
            email: {
                validators: {
                    emailAddress: {
                        message: 'The input is not a valid email address'
                    },
                    notEmpty: {
                        message: 'The username is required and cannot be empty'
                    }
                }
            }
        }
    });


    $('#signup').click(function() {
        var data = {
            "collection": {
                "templates": [
                    {
                        "data": [
                            {
                                "name": "fieldCode",
                                "value":$("#fieldCode").val()
                            },
                            {
                                "name": "fieldName",
                                "value": $("#fieldName").val()
                            },
                            {
                                "name": "crtUserName",
                                "value": $("#crtUserName").val()
                            },
                            {
                                "name": "mntUserName",
                                "value": $("#mntUserName").val()
                            },
                            {
                                "name": "des",
                                "value": $("#des").val()
                            },
                            {
                                "name": "crtUserId",
                                "value": $("#crtUserId").val()
                            },
                            {
                                "name": "mntUserId",
                                "value": $("#mntUserId").val()
                            }
                        ]
                    }
                ]
            }
        }
        if(localStorage.getItem("edit") =="true"){
            $.ajax({
                url: 'http://10.238.119.7:8080/apps/'+localStorage.getItem('appCode')+'/resources/' + localStorage.getItem("resourceCode") +'/definitions/' +localStorage.getItem("definitionCode") +'/fields/' +localStorage.getItem("fieldCode"),
                async: false,//
                type: 'PUT',//PUT DELETE POST
                data:JSON.stringify(data),
                'processData': false,
                contentType: "application/json;charset=utf-8",
                dataType: "text",
                success: function (result) {
                    console.log(result)
//                    var datas = $.ET.toObjectArr(result)
//                    console.log(result.collection.error.message)
                    if(result.indexOf("pps")){
                        layer.msg("修改成功")
                        setTimeout(function(){
                            parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        },500)
                    }else{
                        layer.msg(result.collection.error.message)
                    }
                }, error: function () {
                    alert("网络出错啦")
                }
            })

        }else{
            $.ajax({
                url: 'http://10.238.119.7:8080/apps/'+localStorage.getItem('appCode')+'/resources/'+localStorage.getItem('resourceCode')+'/definitions/'+localStorage.getItem("definitionCode") +'/fields',
                async: false,//
                type: 'POST',//PUT DELETE POST
                data:JSON.stringify(data),
                'processData': false,
                contentType: "application/json;charset=utf-8",
                dataType: "json",
                success: function (result) {
                    if(result == 1){
                        layer.msg("新增成功")
                        setTimeout(function(){
                            parent.location.reload();
                            var index = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(index);
                        },500)
                    }else{
                        layer.msg(result.collection.error.message)
                    }
                }, error: function () {
                    alert("网络出错啦")
                }
            })
        }

//

            return false;
    });
});
</script>
</body>
</html>